//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeLocalReference"/> class represents a value reference
    /// to a local variable in a method.
    /// </summary>
    public class CodeLocalReference : CodeValueReference
    {
        private LocalBuilder m_localBuilder;
        private Type m_type;
        private string m_name;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeLocalReference"/> with
        /// a type and a name.  The name does not need to be unique, but picking
        /// something easily recognized when debugging code is recommended.
        /// </summary>
        /// <param name="type">The type to local variable.</param>
        /// <param name="name">The name of the local variable.</param>
        public CodeLocalReference(Type type, string name)
        {
            m_type = type;
            m_name = name;
        }

        public Type Type
        {
            get { return m_type; }
        }

        public string Name
        {
            get { return m_name; }
        }

        internal LocalBuilder LocalBuilder
        {
            get { return m_localBuilder; }
            set { m_localBuilder = value; }
        }

        public override void Generate(ILGenerator gen)
        {
            gen.Emit(OpCodes.Ldloc, m_localBuilder);
        }

        public override void GenerateSet(ILGenerator gen, CodeExpression value)
        {
            value.Generate(gen);
            CodeGenerationHelper.GenerateSafeConversion(gen, m_type, value.ResultType);
            gen.Emit(OpCodes.Stloc, m_localBuilder);
        }

        public override void PrintCode(CodeWriter cw)
        {
            cw.Write(m_name);
        }

        public override Type ResultType
        {
            get { return m_type; }
        }
    }
}